package com.lovo.pis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.pis.dto.PageBean;
import com.lovo.pis.dto.SupplierBean;
import com.lovo.pis.mapper.SupplierMapper;
import com.lovo.pis.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.List;

@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, SupplierBean> implements ISupplierService {

    @Autowired
    SupplierMapper supplierMapper;

    @Override
    public IPage<SupplierBean> queryByItem(PageBean<SupplierBean> pageInfo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (pageInfo.getData().getName() != null && pageInfo.getData().getName() != "") {
            queryWrapper.like("name", pageInfo.getData().getName());
        }
        if (pageInfo.getData().getScope() != null && pageInfo.getData().getScope() != "") {
            queryWrapper.like("scope", pageInfo.getData().getScope());
        }
        return supplierMapper.selectPage(new Page<>(pageInfo.getCurPage(), pageInfo.getPageSize()), queryWrapper);
    }

    @Override
    public List<SupplierBean> queryAllName() {
        return supplierMapper.selectList(null);
    }

    @Override
    public SupplierBean queryById(int id) {
        return supplierMapper.selectById(id);
    }

    @Override
    public void changeById(SupplierBean supplierBean) {
        supplierMapper.updateById(supplierBean);
    }

    @Override
    public void addSupplier(SupplierBean supplierBean) {
        supplierMapper.insert(supplierBean);
    }

    @Override
    public boolean saveExcelDataTo(List<SupplierBean> list) {
        for (SupplierBean supplierBean : list) {
            supplierMapper.insert(supplierBean);
        }
        return true;
    }

}
